package slices

import (
	"unsafe"
)

// 从slice中挤出个TYPE数据
func Dequeue[TYPE any](slice []byte) (newSlice []byte, value TYPE, ok bool) {
	length := int(unsafe.Sizeof(value))
	if len(slice) >= length {
		value = *(*TYPE)(unsafe.Pointer(&slice[0]))
		newSlice = slice[length:]
		ok = true
	}

	return
}

// slice前面挤出片段
func DequeueBytes(slice []byte, length int) (newSlice []byte, cuted []byte, ok bool) {
	if len(slice) >= length {
		cuted = slice[:length]
		newSlice = slice[length:]
		ok = true
	}

	return
}

// slice前面挤出字符串
func DequeueString(slice []byte, length int) (newSlice []byte, str string, ok bool) {
	if len(slice) >= length {
		str = string(slice[:length])
		newSlice = slice[length:]
		ok = true
	}

	return
}

// 将values插入到slice的from位置. 若from位置不合法将会产生panic
func Enqueue[TYPE any](slice []byte, value TYPE) (newSlice []byte) {
	newSlice = make([]byte, len(slice)+int(unsafe.Sizeof(value)))
	copy(newSlice, slice)
	*(*TYPE)(unsafe.Pointer(&newSlice[len(slice)])) = value
	
	return
}




